/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package data.Imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import data.Services.ImageDAOService;
import model.entity.Image;
import model.entity.Post;

/**
 *
 * @author ninkuhack
 */
public class ImageServiceDAOImp implements ImageDAOService{

    /**
     *
     * @param imageID
     * @return
     */
    private static ImageServiceDAOImp imageDAO;
    
    public static ImageServiceDAOImp getInstance(){
        if(imageDAO == null){
            imageDAO = new ImageServiceDAOImp();
        }
        return imageDAO;
    }
    
    private Image createNewImage(ResultSet rs) throws SQLException{
        Image image = new Image();
        image.setImageID(rs.getInt("image_id"));
        image.setPath(rs.getString("path"));
        image.setPost(PostServiceDAOImp.getInstance().getPostByID(rs.getInt("post_id")));
        return image;
    }
    
    @Override
    public Image getImageByID(int imageID) {
        Image image = null;
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select image_id, path, post_id from images where image_id = ? and status = 1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, imageID);
            rs = statement.executeQuery();
            while(rs.next()){
                image = createNewImage(rs);
            }
            
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return image;
    }

    @Override
    public List<Image> getAllImages() {
        List<Image> list = new ArrayList<>();
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select image_id, path, post_id from images where status = 1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            rs = statement.executeQuery();
            while(rs.next()){
                list.add(createNewImage(rs));
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }

    @Override
    public boolean insertImage(Image image) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "insert into images(path,post_id) values(?,?)";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setString(1, image.getPath());
            statement.setInt(2, image.getPost().getID());
            int row = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public boolean updateImage(Image image) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update images set path = ?, post_id = ? where image_id = ?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setString(1, image.getPath());
            statement.setInt(2, image.getPost().getID());
            statement.setInt(3, image.getImageID());
            int row = statement.executeUpdate();
            if(row > 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public boolean deleteImage(int imageID) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update images set status=0 where image_id = ?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, imageID);
            int row = statement.executeUpdate();
            if(row >= 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }
    
    @Override
    public boolean restoreImage(int imageID) {
        Connection cn = null;
        boolean flag = false;
        try {
            String strSql = "update images set status=1 where image_id = ?";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, imageID);
            int row = statement.executeUpdate();
            if(row >= 0){
                flag = true;
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return flag;
    }

    @Override
    public List<Image> findImageByPostID(int postID) {
        List<Image> list = new ArrayList<>();
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select image_id, path, post_id from images where post_id = ? and status = 1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, postID);
            rs = statement.executeQuery();
            while(rs.next()){
                list.add(createNewImage(rs));
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }
    
    @Override
    public List<Image> findImageByPostID(Post postID) {
        List<Image> list = new ArrayList<>();
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select image_id, path, post_id from images where post_id = ? and status = 1";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            statement.setInt(1, postID.getPost_id());
            rs = statement.executeQuery();
            while(rs.next()){
                list.add(createNewImage(rs));
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }

    @Override
    public ResultSet getImageByResultSet() {
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select image_id, path, post_id from images";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            rs = statement.executeQuery();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return rs;
    }

    @Override
    public Vector<Image> getImageByVector() {
        Vector<Image> list = new Vector<>();
        Connection cn = null;
        ResultSet rs = null;
        try {
            String strSql = "select image_id, path, post_id from images";
            cn = ConnectionManager.getConnection();
            PreparedStatement statement = cn.prepareStatement(strSql);
            rs = statement.executeQuery();
            while(rs.next()){
                Image image = new Image(rs.getInt("image_id"), rs.getString("path"), PostServiceDAOImp.getInstance().getPostByID(rs.getInt("post_id")));
                list.add(image);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }
    
}
